package hn.cch.microservice.service_consumer_multi_param.controller;


import hn.cch.microservice.service_consumer_multi_param.entity.User;
import hn.cch.microservice.service_consumer_multi_param.feign.client.UserFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

//加载配置文件路径
@PropertySource(value = {"classpath:application.properties"})
@RestController
public class UserController {

    //读取配置文件参数
    @Value(value = "${rest.user.url}")
    private String restUserUrl;

    // @Autowired
    // private RestTemplate restTemplate;

    @Autowired
    private UserFeignClient userFeignClient;

    // @Bean
    // public RestTemplate restTemplate() {
    //     RestTemplate restTemplate = new RestTemplate();
    //     return restTemplate;
    // }

    @GetMapping(value = "/{id}")
    public User user(@PathVariable Long id) {
        // return restTemplate.getForObject(restUserUrl + id, User.class);
        return userFeignClient.user(id);
    }


    @GetMapping(value = "/get0")
    public String get0(@RequestParam(value = "account") String account,
                       @RequestParam(value = "name") String name){
        return userFeignClient.get0(account, name);
    }


    @GetMapping(value = "/get1")
    public String get1(@RequestParam(value = "account") String account,
                       @RequestParam(value = "name") String name){
        Map<String, String> map = new HashMap<>();
        map.put("account", account);
        map.put("name", name);
        return userFeignClient.get1(map);
    }


    @PostMapping(value = "/post")
    public User post(@RequestBody User user){
        return userFeignClient.post(user);
    }

}
